#include"foc_init.h"

//INIT
foc_config_t foc_motor1_config_s = {0};
foc_config_t foc_motor2_config_s = {0};

//update
foc_temp_value_t foc_motor1_temp_value_s = {0};
foc_temp_value_t foc_motor2_temp_value_s = {0};

void foc_init(foc_config_t* foc_config_s1,foc_config_t* foc_config_s2)
{
    //Integrate ALL and leave for invocation,Whether to use a structure or not, it can be passed as NULL.
    foc_config_init(foc_config_s1,foc_config_s2);
    foc_pwm_init(foc_config_s1,foc_config_s2);
    foc_peripherals_init(foc_config_s1,foc_config_s2);
}

void foc_config_init(foc_config_t* foc_config_s1,foc_config_t* foc_config_s2)
{
    if(foc_config_s1 != NULL)
    {
        foc_config_s1->foc_mode = FOC_CLOSED_LOOP_DUTY_MODE;
        foc_config_s1->offset_direction = OFFSET_DIRECTION_BACK;
        foc_config_s1->three_phase_channel_s.channel_A = ATOM2_CH3_P00_12;
        foc_config_s1->three_phase_channel_s.channel_B = ATOM2_CH0_P00_9;
        foc_config_s1->three_phase_channel_s.channel_C = ATOM0_CH7_P00_8;
        foc_config_s1->move_max_value_s.max_torque = 100;
        foc_config_s1->move_max_value_s.max_speed = 10000;
        foc_config_s1->max_duty = 1000;
        foc_config_s1->electrode_traction_angle = 120;
        foc_config_s1->pole_pairs = 7;
        foc_config_s1->pwm_freq =  20000;
        foc_config_s1->offset_eletric_angle_length = 0;
    }
    else
    {
        ;
    }
    if(foc_config_s1 != NULL)
    {
        foc_config_s2->foc_mode = FOC_OPEN_LOOP_SPEED_MODE;
        foc_config_s2->offset_direction = OFFSET_DIRECTION_FORWARD;
        foc_config_s2->three_phase_channel_s.channel_A = ATOM1_CH0_P02_8;
        foc_config_s2->three_phase_channel_s.channel_B = ATOM1_CH7_P02_7;
        foc_config_s2->three_phase_channel_s.channel_C = ATOM0_CH6_P02_6;
        foc_config_s2->move_max_value_s.max_torque = 100;
        foc_config_s2->move_max_value_s.max_speed = 10000;
        foc_config_s2->max_duty = 1000;
        foc_config_s2->electrode_traction_angle = 120;
        foc_config_s2->pole_pairs = 7;
        foc_config_s2->pwm_freq =  20000;
        foc_config_s2->offset_eletric_angle_length = 0;
    }
    else
    {
        ;
    }
}

void foc_pwm_init(foc_config_t* foc_config_s1,foc_config_t* foc_config_s2)
{
    if(foc_config_s1 != NULL)
    {
        //Motor A, PWM initialization and interrupt configuration
//        pwm_interpret_init(foc_config_s1->three_phase_channel_s.channel_A,foc_config_s1->pwm_freq,0,FOC_A_ITR_SERVICE,FOC_A_ITR_PRIO);
        pwm_init(foc_config_s1->three_phase_channel_s.channel_A,foc_config_s1->pwm_freq,0);
        pwm_init(foc_config_s1->three_phase_channel_s.channel_B,foc_config_s1->pwm_freq,0);
        pwm_init(foc_config_s1->three_phase_channel_s.channel_C,foc_config_s1->pwm_freq,0);
    }
    else
    {
        ;
    }
    if(foc_config_s2 != NULL)
    {
        //Motor B, PWM initialization and interrupt configuration
//        pwm_interpret_init(foc_config_s2->three_phase_channel_s.channel_A,foc_config_s2->pwm_freq,0,FOC_B_ITR_SERVICE,FOC_B_ITR_PRIO);
        pwm_init(foc_config_s2->three_phase_channel_s.channel_A,foc_config_s2->pwm_freq,0);
        pwm_init(foc_config_s2->three_phase_channel_s.channel_B,foc_config_s2->pwm_freq,0);
        pwm_init(foc_config_s2->three_phase_channel_s.channel_C,foc_config_s2->pwm_freq,0);
    }
    else
    {
        ;
    }
}

void foc_peripherals_init(foc_config_t* foc_config_s1,foc_config_t* foc_config_s2)
{
    if(foc_config_s1 != NULL)
    {
        switch(foc_config_s1->foc_mode)
        {
            case FOC_OPEN_LOOP_DUTY_MODE:
                break;
            case FOC_OPEN_LOOP_ACC_MODE:
                break;
            case FOC_OPEN_LOOP_SPEED_MODE:
                break;
            case FOC_CLOSED_LOOP_DUTY_MODE:
                //注意函数内SPI的数量和序号
                menc15a_init();
                break;
            case FOC_CLOSED_LOOP_ACC_MODE:
                break;
            case FOC_CLOSED_LOOP_SPEED_MODE:
                break;
            case FOC_CLOSED_LOOP_DISTANCE_MODE:
                //注意函数内SPI的数量和序号
                menc15a_init();
                break;
        }
    }
    else
    {
        ;
    }
    if(foc_config_s2 != NULL)
    {
        switch(foc_config_s2->foc_mode)
        {
            case FOC_OPEN_LOOP_DUTY_MODE:
                break;
            case FOC_OPEN_LOOP_ACC_MODE:
                break;
            case FOC_OPEN_LOOP_SPEED_MODE:
                break;
            case FOC_CLOSED_LOOP_DUTY_MODE:
                break;
            case FOC_CLOSED_LOOP_ACC_MODE:
                break;
            case FOC_CLOSED_LOOP_SPEED_MODE:
                break;
            case FOC_CLOSED_LOOP_DISTANCE_MODE:
                break;
        }
    }
    else
    {
        ;
    }
}
